/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.great.ad.DAO;

import br.ufc.great.ad.Model.DB;
import br.ufc.great.ad.Model.Evaluation;
import br.ufc.great.ad.Model.Factors;
import br.ufc.great.ad.Model.Level;
import br.ufc.great.ad.Model.Metric;
import br.ufc.great.ad.Model.Parameters;
import java.util.List;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author great
 */
public class EvaluationDAO {

    DB db = new DB();
    Session session = db.getSession();

    public void addMetric(Metric metric) {
        Transaction tx = (Transaction) session.beginTransaction();
        session.save(metric);
        tx.commit();
    }

    public void addSystem(br.ufc.great.ad.Model.System system) {
        Transaction tx = (Transaction) session.beginTransaction();
        session.save(system);
        tx.commit();
    }

    public void addParameters(Parameters parameters) {
        Transaction tx = (Transaction) session.beginTransaction();
        session.save(parameters);
        tx.commit();
    }

    public void addEvaluation(Evaluation evaluation) {
        Transaction tx = (Transaction) session.beginTransaction();
        session.save(evaluation);
        tx.commit();
    }

    public Factors addFactors(Factors factors) {
        Transaction tx = (Transaction) session.beginTransaction();
        session.save(factors);
        tx.commit();
        
        Query query = (Query) session.createQuery("from Factors");
        List<Factors> factorsList = query.list();
        
        for(Factors f: factorsList)
        {
            if(f.getIdevaluation() == factors.getIdevaluation())
            {
                return f;
            }
        }
        
        return null;
        
    }

    public void addLevel(Level level) {
        Transaction tx = (Transaction) session.beginTransaction();
        session.save(level);
        tx.commit();
    }

    public Evaluation getLastEvaluation() {
        Transaction tx = (Transaction) session.beginTransaction();
        Query query = (Query) session.createQuery("from Evaluation");
        List listEvaluations = query.list();
        tx.commit();
        return (Evaluation) listEvaluations.get(listEvaluations.size() - 1); //Gambiarra? Procurar outra forma de devolver o último elemento cadastrado.
    }
}
